import dayjs from 'dayjs';
import { useGlobals } from 'storybook/preview-api'
import { untrack } from 'svelte';
import { locale } from 'svelte-i18n';

export const LocaleDecorator = (story, context) => {
    const [{locale}] = useGlobals();
    untrack(() => {
        switchLocale(locale);
    })
    return story();
}

export const switchLocale = (newLocale: string) => {
    locale.set(newLocale);
    // 切换全局语言为简体中文
    if (newLocale === 'zh-CN') {
        dayjs.locale('zh-cn');
    } else {
        dayjs.locale('en');
    }
}